home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / IATHREAD.PAK / DISPATCH.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  6KB  |  175 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993-1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE:   dispatch.c
  9. //
  10. //  PURPOSE:  Implement the generic message and command dispatchers.
  11. //    
  12. //
  13. //  FUNCTIONS:
  14. //    DispMessage - Call the function associated with a message.
  15. //    DispCommand - Call the function associated with a command.
  16. //    DispDefault - Call the appropriate default window procedure.
  17. //
  18. //  COMMENTS:
  19. //
  20.  
  21. #include <windows.h>            // required for all Windows applications
  22. #include <windowsx.h>
  23. #include "globals.h"            // prototypes specific to this application
  24.  
  25. LRESULT DispDefault(EDWP, HWND, UINT, WPARAM, LPARAM);
  26.  
  27. //
  28. //  FUNCTION: DispMessage(LPMSDI, HWND, UINT, WPARAM, LPARAM)
  29. //
  30. //  PURPOSE: Call the function associated with a message.
  31. //
  32. //  PARAMETERS:
  33. //    lpmsdi   - Structure containing the message dispatch information.
  34. //    hwnd     - The window handle
  35. //    uMessage - The message number
  36. //    wparam   - Message specific data
  37. //    lparam   - Message specific data
  38. //
  39. //  RETURN VALUE:
  40. //    The value returned by the message function that was called.
  41. //
  42. //  COMMENTS:
  43. //    Runs the table of messages stored in lpmsdi->rgmsd searching
  44. //    for a message number that matches uMessage.  If a match is found,
  45. //    call the associated function.  Otherwise, call DispDefault to
  46. //    call the default function, if any, associated with the message
  47. //    structure.  In either case, return the value recieved from the
  48. //    message or default function.
  49. //
  50.  
  51. LRESULT DispMessage(LPMSDI lpmsdi, 
  52.                     HWND   hwnd, 
  53.                     UINT   uMessage, 
  54.                     WPARAM wparam, 
  55.                     LPARAM lparam)
  56. {
  57.      int  imsd;
  58.  
  59.     MSD *rgmsd = lpmsdi->rgmsd;
  60.     int  cmsd  = lpmsdi->cmsd;
  61.  
  62.     for (imsd = 0; imsd < cmsd; imsd++)
  63.     {
  64.         if (rgmsd[imsd].uMessage == uMessage)
  65.             return rgmsd[imsd].pfnmsg(hwnd, uMessage, wparam, lparam);
  66.     }
  67.  
  68.     return DispDefault(lpmsdi->edwp, hwnd, uMessage, wparam, lparam);
  69. }
  70.  
  71. //
  72. //  FUNCTION: DispCommand(LPCMDI, HWND, WPARAM, LPARAM)
  73. //
  74. //  PURPOSE: Call the function associated with a command.
  75. //
  76. //  PARAMETERS:
  77. //    lpcmdi    - Structure containing the command dispatch information.
  78. //    hwnd      - The window handle
  79. //    wParam    - Parameter. See below
  80. //    lParam    - Parameter. See below
  81. //
  82. //    The following macros are used to extract the parameters passed to
  83. //    WM_COMMAND handlers.  They are implemented for convenience and to
  84. //    ease porting from Win16 to Win32.
  85. //
  86. //        GET_WM_COMMAND_ID(wparam, lparam)   - Identifier of the menu item,
  87. //                                              control, or accelerator.
  88. //        GET_WM_COMMAND_CMD(wparam, lparam)  - Notification code.
  89. //        GET_WM_COMMAND_HWND(wparam, lparam) - The control handle or NULL.
  90. // 
  91. //  RETURN VALUE:
  92. //    The value returned by the command function that was called.
  93. //
  94. //  COMMENTS:
  95. //    Runs the table of commands stored in lpcmdi->rgcmd searching
  96. //    for a command number that matches wCommand.  If a match is found,
  97. //    call the associated function.  Otherwise, call DispDefault to
  98. //    call the default function, if any, associated with the command
  99. //    structure.  In either case, return the value recieved from the
  100. //    command or default function.
  101. //
  102.  
  103. LRESULT DispCommand(LPCMDI lpcmdi, 
  104.                           HWND   hwnd,
  105.                           WPARAM wparam,
  106.                           LPARAM lparam)
  107. {
  108.     WORD    wCommand = GET_WM_COMMAND_ID(wparam, lparam);
  109.     int     icmd;
  110.  
  111.     CMD    *rgcmd = lpcmdi->rgcmd;
  112.     int     ccmd  = lpcmdi->ccmd;
  113.  
  114.     // Message packing of wparam and lparam have changed for Win32,
  115.     // so use the GET_WM_COMMAND macro to unpack the commnad
  116.  
  117.     for (icmd = 0; icmd < ccmd; icmd++)
  118.     {
  119.         if (rgcmd[icmd].wCommand == wCommand)
  120.         {
  121.             return rgcmd[icmd].pfncmd(hwnd,
  122.                                       wCommand,
  123.                                       GET_WM_COMMAND_CMD(wparam, lparam),
  124.                                       GET_WM_COMMAND_HWND(wparam, lparam));
  125.         }
  126.     }
  127.  
  128.     return DispDefault(lpcmdi->edwp, hwnd, WM_COMMAND, wparam, lparam);
  129. }
  130.  
  131.  
  132. //
  133. //  FUNCTION: DispDefault(EDWP, HWND, UINT, WPARAM, LPARAM)
  134. //
  135. //  PURPOSE: Call the appropriate default window procedure.
  136. //
  137. //  PARAMETERS:
  138. //    edwp     - Enumerate specifying the appropriate default winow procedure.
  139. //    hwnd     - The window handle
  140. //    uMessage - The message number
  141. //    wparam   - Message specific data
  142. //    lparam   - Message specific data
  143. //
  144. //  RETURN VALUE:
  145. //    If there is a default proc, return the value returned by the
  146. //    default proc.  Otherwise, return 0.
  147. //
  148. //  COMMENTS:
  149. //    Calls the default procedure associated with edwp using the specified
  150. //    parameters.
  151. //
  152.  
  153. LRESULT DispDefault(EDWP   edwp, 
  154.                     HWND   hwnd, 
  155.                     UINT   uMessage, 
  156.                     WPARAM wparam, 
  157.                     LPARAM lparam)
  158. {
  159.     switch (edwp)
  160.     {
  161.         case edwpNone:
  162.             return 0;
  163.         case edwpWindow:
  164.             return DefWindowProc(hwnd, uMessage, wparam, lparam);
  165.         case edwpDialog:
  166.             return DefDlgProc(hwnd, uMessage, wparam, lparam);
  167.         case edwpMDIFrame:
  168.             return DefFrameProc(hwnd, ghwndMDIClient, uMessage, wparam, 
  169.                                 lparam);
  170.         case edwpMDIChild:
  171.             return DefMDIChildProc(hwnd, uMessage, wparam, lparam);
  172.     }
  173.     return 0;
  174. }
  175.